package com.app.twth.ui.b2bpick.category;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityB2bPickCategoryBinding;
import com.app.twth.ui.b2bpick.B2BPick;
import com.app.twth.ui.b2bpick.B2BPickViewModel;
import com.app.twth.ui.b2bpick.submit.B2BPickSubmitActivity;
import com.app.twth.ui.b2cpick.B2CPickDetails;
import com.app.twth.ui.b2cpick.category.B2CPickCategoryAdapter;
import com.app.twth.ui.b2cpick.submit.B2CPickSubmit;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.FastJsonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @describe :
 */
public class B2BPickCategoryActivity extends BaseActivity<ActivityB2bPickCategoryBinding, B2BPickViewModel> {
    private String TAG = getClass().getSimpleName();
    B2CPickCategoryAdapter adapter;

    @Override
    protected void initListener() {
        viewModel.pickLiveData.observe(this, data -> {
            adapter.setData(data.getDetailList(), 0);

        });
        viewModel.skuDetailsLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("B2CPickSubmitQr", viewModel.pickSubmitQrLiveData.getValue());
            bundle.putString("data", FastJsonUtil.createJsonString(data));
            AppRouter.showActivityForResult(mContext, B2BPickSubmitActivity.class, bundle, Constant.ForResult2);

        });

        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Pickup(Category)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        viewModel.pickLiveData.setValue((B2BPick) bundle.getSerializable("B2BPick"));
        adapter = new B2CPickCategoryAdapter(this, viewModel.pickLiveData.getValue().getDetailList(), (one, two) -> {//sku
            B2CPickDetails tempPickUpBean = viewModel.pickLiveData.getValue().getDetailList().get(one);
            B2CPickDetails.SkuDetailListDTO pickSkuDetails = tempPickUpBean.getSkuDetailList().get(two);
            List<B2CPickDetails.LocationLpnDTO> pickLocationDetails = pickSkuDetails.getLocationLpnDTOS();

            if (pickLocationDetails == null || pickLocationDetails.size() == 0) {
                Log.e(TAG, "aaaaaaaaaaa");
                viewModel.b2bPickSkuInventoryDetail(one, two, pickSkuDetails.getSku());
            } else {
                Log.e(TAG, "bbbbbbb:" + pickLocationDetails.size());
                pickSkuDetails.setClick(!pickSkuDetails.isClick());
                adapter.notifyDataSetChanged();
            }
        }, lpn -> {//lpn
            viewModel.b2bPickCheckIbLpn(lpn);
        });
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerview.setLayoutManager(layoutManager1);
        binding.recyclerview.setAdapter(adapter);

        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCode.setText("");
                bottomLayoutBinding.tvHint.setText("");
                break;
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        viewModel.b2bPickCheckIbLpn(barcodeStr);
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                EditTextUtil.hideInput(mContext);
                viewModel.b2bPickCheckIbLpn(barcodeStr);
            }
            return false;
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            try {
                B2CPickSubmit b2CPickSubmit = (B2CPickSubmit) data.getSerializableExtra("data");

                List<B2CPickDetails> pickDetailList = b2CPickSubmit.getPickDetailList();
                List<B2CPickDetails.SkuDetailListDTO> pickSkuDetailList = b2CPickSubmit.getPickSkuDetailList();

                if (pickDetailList == null) {
                    pickDetailList = new ArrayList<>();
                }
                if (pickSkuDetailList == null) {
                    pickSkuDetailList = new ArrayList<>();
                }
                if ((pickDetailList.size() == 0) && (pickSkuDetailList.size() == 0)) {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                    return;
                }
                viewModel.pickLiveData.getValue().setDetailList(pickDetailList);
                adapter.setData(viewModel.pickLiveData.getValue().getDetailList(), 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
